package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.FinanceDao;
import cn.itcast.dao.cargo.InvoiceDao;
import cn.itcast.domain.cargo.Finance;
import cn.itcast.domain.cargo.FinanceExample;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.service.cargo.FinanceService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
@Service(timeout = 100000)
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    private FinanceDao financeDao;

    @Autowired
    private InvoiceDao invoiceDao;
    /**
     * 分页查询
     *
     * @param financeExample 分页查询的参数
     * @param pageNum        当前页
     * @param pageSize       页大小
     * @return
     */
    @Override
    public PageInfo<Finance> findByPage(FinanceExample financeExample, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Finance> list = financeDao.selectByExample(financeExample);
        PageInfo<Finance> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 查询所有
     *
     * @param financeExample
     */
    @Override
    public List<Finance> findAll(FinanceExample financeExample) {
        return financeDao.selectByExample(financeExample);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public Finance findById(String id) {
        Finance finance = financeDao.selectByPrimaryKey(id);
        return finance;
    }

    /**
     * 新增
     *
     * @param finance
     */
    @Override
    public void save(Finance finance) {
        Invoice invoice = invoiceDao.selectByPrimaryKey(finance.getInvoiceId());
        invoice.setStatus(1);
        invoiceDao.updateByPrimaryKey(invoice);
        financeDao.insertSelective(finance);
    }

    /**
     * 修改
     *
     * @param finance
     */
    @Override
    public void update(Finance finance) {
        financeDao.insertSelective(finance);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        financeDao.deleteByPrimaryKey(id);
    }
}
